var ctx=window.daijia.ctx;
var off_set=null;
$(document).ready(function(){
	
	var newDate = new Date();
	var startDate = new Date();
	startDate.setDate(startDate.getDate()-30);
	var startTimeStr=startDate.getFullYear()+"-"+((startDate.getMonth()+1)>9?(startDate.getMonth()+1):'0' + (startDate.getMonth()+1))+"-"+(startDate.getDate()>9?startDate.getDate():'0' + startDate.getDate())+" "+"00:00";
	var endTimeStr=newDate.getFullYear()+"-"+((newDate.getMonth()+1)>9?(newDate.getMonth()+1):'0' + (newDate.getMonth()+1))+"-"+(newDate.getDate()>9?newDate.getDate():'0' + newDate.getDate())+" "+"00:00";
	$("#startTime").val(startTimeStr);
	$("#endTime").val(endTimeStr);
	
	$("#startTime").datetimepicker({
        format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	$("#endTime").datetimepicker({
		format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	
	initPagedata(getTotalElements(),true);
	
	//点击搜索
	$("#search_button").click(function(){
		if($("#startTime").val()=="" || $("#endTime").val()==""){
			$.scojs_message("日期不能为空", $.scojs_message.TYPE_ERROR);
			return false;
		}
		var DATE_FORMAT = /^[0-9]{4}-[0-1]{1}[0-9]{1}-[0-3]{1}[0-9]{1} [0-2]{1}[0-9]{1}:[0-5]{1}[0-9]{1}$/;
		if(!DATE_FORMAT.test($("#startTime").val()) || !DATE_FORMAT.test($("#endTime").val())){
		   $.scojs_message("您输入的日期格式有误，正确格式应为：2012-01-01 08:30", $.scojs_message.TYPE_ERROR);
		   return false;
		}
		if($("#startTime").val()>$("#endTime").val()){
			 $.scojs_message("开始日期不能大于结束日期", $.scojs_message.TYPE_ERROR);
			 return false;
		}
		
		$("#dateRangeText").text($("#startTime").val()+" 至 "+$("#endTime").val());
		$("#dateRangeButton").parent().find(".dropdown-calendar").hide();
		
		var newTotalElements=getTotalElements();
		initPagedata(newTotalElements);
		
			$("#daijiapage").find("li a").html("共 "+newTotalElements+" 条数据");
		
	});

	//导出
	$("#daijia_export_button").click(function(){
		
		window.location=ctx+"/chart/exportMemberOrder?pager.offset="+off_set+"&startTime="+$("#startTime").val()+
						"&endTime="+$("#endTime").val()+(isMain?"&companyId="+$("#companyId").val():"")+"&gradeId="+$("#grade").val()+
						"&type="+$("#order").val();
	});
	
});

	
	
	function loadData(page){
		
		if($('#loadblacklayer').length==0){
			var dw = $("#daijia").width();
			var dh = $("#daijia").height();
			var backlayer ="<div id='loadblacklayer'><div style='margin-top:"+(dh/2)+"px;margin-left:"+(dw/2)+"px;'><img src='"+ctx+"/assets/css/images/loading.gif' /></div></div>"
			$("#daijia tbody").append(backlayer);
			$('#loadblacklayer').css({"z-index":"9","background":"#000","opacity":"0.5","position":'absolute',"top":0,"width":dw,"height":dh});
		}
		
		off_set=(page-1)*20;
		var newcontent = '';
		var newTotalElements=0;
		
		$.ajax({
	        url : ctx+'/chart/memberOrderData',
	        type:'GET',
	        async:true,
	        dataType : 'json',
	        data : {
	        	"pager.offset":off_set,
	        	"startTime":$("#startTime").val(),
	        	"endTime":$("#endTime").val(),
	        	"companyId":$("#companyId").val(),
	        	"gradeId":$("#grade").val(),
	        	"type":$("#order").val()
	        },
	        success : function(result){
	        	
	        	if(result.totalElements>0){
	        		newcontent+="";
	        		$(result.content).each(function(index,data){
	        			
						if(data.name.indexOf("总计")!=-1){
							newcontent+="<tr class='content warning grid-data tr-hui'>"+
						    "<td>"+data.name+"</br>"+data.phone+"</td>"+
							"<td>"+data.companyName+"</td>"+
							"<td><span style='color:#ff950b;font-weight:700;'>"+data.finishNums+"</span></td>"+
							"<td><span style='color:#ff950b;font-weight:700;'>"+data.money.toFixed(1)+"</span></td>"+
							"</tr>";
						   }else{
							   	newcontent+="<tr class='content grid-data tr-hui'>"+
							    "<td>"+data.name+"</br>"+data.phone+"</td>"+
								"<td>"+data.companyName+"</td>"+
								"<td><span style='color:#ff950b;font-weight:700;'>"+data.finishNums+"</span></td>"+
								"<td><span style='color:#ff950b;font-weight:700;'>"+data.money.toFixed(1)+"</span></td>"+
								"</tr>";
						   }
						        		
	        			
	        		});
	        		 newcontent+="</tbody>";
	        	}
	        	$('#loadblacklayer').remove();
	        	$("#daijia tbody").remove();
	        	$("#daijia").append(newcontent);
	        	$("#daijia tbody").show(300);
	    		newTotalElements=result.totalElements;
	    		$("#daijiapage").find("li a").html("共 "+newTotalElements+" 条数据");
	        }
		});
		
		return newTotalElements;
	}
	//分页初始化
	function initPagedata(total){
		$("#daijiadata").paging(total, {
			format: '[< ncnnn >]',
			perpage: 20,
			onSelect: function (page) {
				loadData(page);
		    	return false;
			},
			onFormat: function (type) {
				switch (type) {
				case 'block': // n and c
					if (!this.active)
						return '';
					else if (this.value != this.page)
						return '<li><a href="#">' + this.value + '</a></li>';
					return '<li class="active"><a href="#">' + this.value + '</a></li>';
				case 'next': // >
					return '<li><a href="#">&rsaquo;</a><li>';
				case 'prev': // <
						return '<li><a href="#">&lsaquo;</a><li>';
				case 'first': // [
						return '<li><a href="#">&laquo;</a><li>';
				case 'last': // ]
						return '<li><a href="#">&raquo;</a></li>';
				}
			}
		});
	}
   

function getTotalElements(){
	
	$.ajax({
        url : ctx+'/chart/memberOrderData',
        type:'GET',
        async:false,
        dataType : 'json',
        data : {
        	"companyId":$("#companyId").val(),
        	"memberName":$("#memberName").val(),
        	"gradeId":$("#grade").val(),
        	"type":$("#order").val()
        },
        success : function(result){
        	newTotalElements=result.totalElements;
        }
	});
	
	return newTotalElements;
	
}

